package com.domain.common.json;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * ClassName: JacksonUtil<br>
 * Description: JSON字符串处理.<br>
 * 注意这里使用的fasterxml.jackson<br>
 *
 * @author Sean
 * @date: 2016年11月17日 下午5:56:59
 * @version V1.0
 * @since JDK 1.8
 */
public class JacksonUtil {
	private static ObjectMapper objectMapper;

	public static ObjectMapper getMapper() {
		if (null == objectMapper) {
			objectMapper = new ObjectMapper().setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
			objectMapper.configure(MapperFeature.ALLOW_EXPLICIT_PROPERTY_RENAMING, false);
		}
		return objectMapper;
	}

	public static String getString(String content, String fieldName) {
		if (null == content)
			return null;
		JsonNode node = null;
		try {
			node = getMapper().readTree(content);
		} catch (IOException e) {
			return null;
		}
		if (null == node) {
			return null;
		}
		if (node.has(fieldName)) {
			return node.get(fieldName).asText();
		}
		return null;
	}

	public static <T> String tToString(T t) {

		try {
			return getMapper().writeValueAsString(t);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取对象 集合除外 这是针对ApiResult data为对象的情况<br>
	 * 如有需要请自己添加自己的方法 <br>
	 * 添加时请考虑通用性
	 * 
	 * @param content
	 * @param elementClass
	 * @return
	 */
	public static <T> T getApiEntity(String content, Class<T> elementClass) {
		if (null == content) {
			return null;
		}
		JsonNode node = null;
		try {
			node = getMapper().readTree(content);
		} catch (IOException e) {
			return null;
		}
		if (null == node) {
			return null;
		}
		if (node.has("data") && !node.get("data").isArray()) {
			return getMapper().convertValue(node.get("data"), elementClass);
		}
		return null;
	}

	public static <T> T getEntity(String content, Class<T> elementClass) {
		if (null == content) {
			return null;
		}
		JsonNode node = null;
		try {
			node = getMapper().readTree(content);
		} catch (IOException e) {
			return null;
		}
		if (null == node) {
			return null;
		}
		return getMapper().convertValue(node, elementClass);
	}

	/**
	 * 获取集合
	 * 
	 * @param content
	 * @param elementClass
	 * @return
	 */
	public static <T> List<T> getApiList(String content, Class<T> elementClass) {
		if (null == content) {
			return null;
		}
		JsonNode node = null;
		try {
			node = getMapper().readTree(content);
		} catch (IOException e) {
			return null;
		}

		if (node.has("data") && node.get("data").isArray()) {
			JavaType type = getMapper().getTypeFactory().constructCollectionType(ArrayList.class, elementClass);
			return getMapper().convertValue(node.get("data"), type);
		}
		return new ArrayList<T>();
	}

	/**
	 * 获取集合
	 * 
	 * @param content
	 * @param elementClass
	 * @return
	 */
	public static <T> List<T> getList(String content, Class<T> elementClass) {
		if (null == content) {
			return null;
		}
		JsonNode node = null;
		try {
			node = getMapper().readTree(content);
		} catch (IOException e) {
			return null;
		}
		if (node.isArray()) {
			JavaType type = getMapper().getTypeFactory().constructCollectionType(ArrayList.class, elementClass);
			return getMapper().convertValue(node, type);
		}
		return new ArrayList<T>();
	}

}
